package com.zc.framework.system;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.StringValueResolver;

import java.util.*;

/**
 * Created by zc on 2016/8/31.
 */
public class AppConfig extends PropertyPlaceholderConfigurer
        implements  Map<String, String>, PropertyAccessor {

    Map<String, String> props = new HashMap<>();
    final Properties sysProperties = System.getProperties();

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) throws BeansException {
        this.props.clear();
        for (Entry<Object, Object> e: props.entrySet())
        {
            this.props.put(e.getKey().toString(), e.getValue().toString());
            sysProperties.put(e.getKey().toString(), e.getValue().toString());
        }
        super.processProperties(beanFactoryToProcess, props);
    }

    protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess, StringValueResolver valueResolver) {
//        super.doProcessProperties(beanFactoryToProcess, valueResolver);
        for(Entry<String, String> e: props.entrySet()) {
            e.setValue(valueResolver.resolveStringValue(e.getValue()));
        }
    }

    @Override
    public int size() {
        return props.size();
    }

    @Override
    public boolean isEmpty() {
        return props.isEmpty();
    }

    @Override
    public boolean containsKey(Object key) {
        return props.containsKey(key);
    }

    @Override
    public boolean containsValue(Object value) {
        return props.containsValue(value);
    }

    @Override
    public String get(Object key) {
        return props.get(key);
    }

    @Override
    public String put(String key, String value) {
        throw new UnsupportedOperationException();
    }

    @Override
    public String remove(Object key) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void putAll(Map<? extends String, ? extends String> m) {
        throw new UnsupportedOperationException();
    }

    @Override
    public void clear() {
        throw new UnsupportedOperationException();
    }

    @Override
    public Set<String> keySet() {
        return props.keySet();
    }

    @Override
    public Collection<String> values() {
        return props.values();
    }

    @Override
    public Set<Entry<String, String>> entrySet() {
        return props.entrySet();
    }

    @Override
    public String getProperty(String key) {
        return this.get(key);
    }
}
